home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / DYNLIST.C < prev    next >
Text File  |  1989-12-30  |  1KB  |  43 lines

  1. main()
  2. {
  3. struct animal {
  4.    char name[25];
  5.    char breed[25];
  6.    int age;
  7. } *pet1, *pet2, *pet3;
  8.  
  9.    pet1 = (struct animal *)malloc(sizeof(struct animal));
  10.    strcpy(pet1->name,"General");
  11.    strcpy(pet1->breed,"Mixed Breed");
  12.    pet1->age = 1;
  13.  
  14.    pet2 = pet1;   /* pet2 now points to the above data structure */
  15.  
  16.    pet1 = (struct animal *)malloc(sizeof(struct animal));
  17.    strcpy(pet1->name,"Frank");
  18.    strcpy(pet1->breed,"Labrador Retriever");
  19.    pet1->age = 3;
  20.  
  21.    pet3 = (struct animal *)malloc(sizeof(struct animal));
  22.    strcpy(pet3->name,"Krystal");
  23.    strcpy(pet3->breed,"German Shepherd");
  24.    pet3->age = 4;
  25.  
  26.        /* now print out the data described above */
  27.  
  28.    printf("%s is a %s, and is %d years old.\n", pet1->name,
  29.            pet1->breed, pet1->age);
  30.  
  31.    printf("%s is a %s, and is %d years old.\n", pet2->name,
  32.            pet2->breed, pet2->age);
  33.  
  34.    printf("%s is a %s, and is %d years old.\n", pet3->name,
  35.            pet3->breed, pet3->age);
  36.  
  37.    pet1 = pet3;   /* pet1 now points to the same structure that
  38.                       pet3 points to                           */
  39.    free(pet3);    /* this frees up one structure               */
  40.    free(pet2);    /* this frees up one more structure          */
  41. /* free(pet1);    this cannot be done, see explanation in text */
  42. }
  43.